package com.jy.horny.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.jy.horny.R
import com.jy.horny.adapter.RegisterDateAdapter
import com.lib.module_base.utils.getCurrentYear

/**
 * 生日修改弹窗
 */
class SelectBirthDialog private constructor(context: Context, theme: Int) : Dialog(context, theme) {

    override fun dismiss() {
        super.dismiss()
    }

    interface Callback {
        fun onBackClick(value: String?)
    }

    private class Params {
        lateinit var context: Context
        var hasShadow = true
        var canCancel = true
        var callback: Callback? = null
        var month: String? = null
        var day: String? = null
        var year: String? = null
        val adapter by lazy { RegisterDateAdapter() }
        val yearList by lazy { (1930..getCurrentYear().toInt()).toList().map { it.toString() } }
    }

    class Builder(context: Context) {
        private val p = Params()

        init {
            p.context = context
        }

        fun setCallback(callback: Callback, month: String?, day: String?, year: String?): Builder {
            p.callback = callback
            p.year = year
            p.month = month
            p.day = day
            return this
        }

        fun create(): SelectBirthDialog {
            val dialog = SelectBirthDialog(
                p.context,
                if (p.hasShadow) com.lib.module_res.R.style.Theme_Light_NoTitle_Dialog else com.lib.module_res.R.style.Theme_Light_NoTitle_NoShadow_Dialog
            )

            val window = dialog.window
            if (window != null) {
                window.setWindowAnimations(com.lib.module_res.R.style.Animation_Bottom_Rising)
                window.decorView.setPadding(0, 0, 0, 0)
                val lp = window.attributes
                lp.width = WindowManager.LayoutParams.MATCH_PARENT
                lp.height = WindowManager.LayoutParams.MATCH_PARENT
                window.attributes = lp
                window.setGravity(Gravity.CENTER)
            }

            @SuppressLint("InflateParams") val view = LayoutInflater.from(p.context)
                .inflate(R.layout.dialog_select_birth, null)

            initialViews(view, dialog)

            dialog.setContentView(view)
            dialog.setCanceledOnTouchOutside(p.canCancel)
            dialog.setCancelable(p.canCancel)
            return dialog
        }

        fun show() {
            create().show()
        }

        private fun initialViews(view: View, dialog: SelectBirthDialog) {
            val vSelectBirthMonth = view.findViewById<View>(R.id.vSelectBirthMonth)
            val txtSelectBirthMonth = view.findViewById<TextView>(R.id.txtSelectBirthMonth)
            val imgSelectBirthMonthExpand =
                view.findViewById<ImageView>(R.id.imgSelectBirthMonthExpand)

            val vSelectBirthDay = view.findViewById<View>(R.id.vSelectBirthDay)
            val txtSelectBirthDay = view.findViewById<TextView>(R.id.txtSelectBirthDay)
            val imgSelectBirthDayExpand = view.findViewById<ImageView>(R.id.imgSelectBirthDayExpand)

            val vSelectBirthYear = view.findViewById<View>(R.id.vSelectBirthYear)
            val txtSelectBirthYear = view.findViewById<TextView>(R.id.txtSelectBirthYear)
            val imgSelectBirthYearExpand =
                view.findViewById<ImageView>(R.id.imgSelectBirthYearExpand)

            val rvSelectBirthMonth = view.findViewById<RecyclerView>(R.id.rvSelectBirthMonth)
            val rvSelectBirthDay = view.findViewById<RecyclerView>(R.id.rvSelectBirthDay)
            val rvSelectBirthYear = view.findViewById<RecyclerView>(R.id.rvSelectBirthYear)
            val txtSelectBirthCancel = view.findViewById<TextView>(R.id.txtSelectBirthCancel)
            val txtSelectBirthSave = view.findViewById<TextView>(R.id.txtSelectBirthSave)

            if (!TextUtils.isEmpty(p.month)) {
                txtSelectBirthMonth.text = p.month
            }
            if (!TextUtils.isEmpty(p.day)) {
                txtSelectBirthDay.text = p.day
            }
            if (!TextUtils.isEmpty(p.year)) {
                txtSelectBirthYear.text = p.year
            }

            txtSelectBirthCancel.setOnClickListener {
                //取消
                dialog.dismiss()
            }
            txtSelectBirthSave.setOnClickListener {
                //保存
                p.callback?.onBackClick("${txtSelectBirthMonth.text}-${txtSelectBirthDay.text}-${txtSelectBirthYear.text}")
                dialog.dismiss()
            }
            vSelectBirthMonth.setOnClickListener {
                //点击月份
                rvSelectBirthMonth.visibility = View.VISIBLE
                rvSelectBirthDay.visibility = View.INVISIBLE
                rvSelectBirthYear.visibility = View.INVISIBLE
                imgSelectBirthMonthExpand.setImageResource(com.lib.module_res.R.mipmap.img_register_up)
                imgSelectBirthDayExpand.setImageResource(com.lib.module_res.R.mipmap.img_register_down)
                imgSelectBirthYearExpand.setImageResource(com.lib.module_res.R.mipmap.img_register_down)
                rvSelectBirthMonth.layoutManager = LinearLayoutManager(p.context)
                p.adapter.set(
                    p.context.resources.getStringArray(R.array.month_array).toMutableList(), 0
                )
                rvSelectBirthMonth.adapter = p.adapter
            }
            vSelectBirthDay.setOnClickListener {
                //点击日
                rvSelectBirthMonth.visibility = View.INVISIBLE
                rvSelectBirthDay.visibility = View.VISIBLE
                rvSelectBirthYear.visibility = View.INVISIBLE
                imgSelectBirthMonthExpand.setImageResource(com.lib.module_res.R.mipmap.img_register_down)
                imgSelectBirthDayExpand.setImageResource(com.lib.module_res.R.mipmap.img_register_up)
                imgSelectBirthYearExpand.setImageResource(com.lib.module_res.R.mipmap.img_register_down)
                rvSelectBirthDay.layoutManager = LinearLayoutManager(p.context)
                p.adapter.set(
                    p.context.resources.getStringArray(R.array.day_array).toMutableList(), 1
                )
                rvSelectBirthDay.adapter = p.adapter
            }
            vSelectBirthYear.setOnClickListener {
                //点击年
                rvSelectBirthMonth.visibility = View.INVISIBLE
                rvSelectBirthDay.visibility = View.INVISIBLE
                rvSelectBirthYear.visibility = View.VISIBLE
                imgSelectBirthMonthExpand.setImageResource(com.lib.module_res.R.mipmap.img_register_down)
                imgSelectBirthDayExpand.setImageResource(com.lib.module_res.R.mipmap.img_register_down)
                imgSelectBirthYearExpand.setImageResource(com.lib.module_res.R.mipmap.img_register_up)
                rvSelectBirthYear.layoutManager = LinearLayoutManager(p.context)
                p.adapter.set(
                    p.yearList, 2
                )
                rvSelectBirthYear.adapter = p.adapter
            }
            //点击年月日的item事件
            p.adapter.setOnItemClickListener(object : RegisterDateAdapter.OnItemClickListener {
                override fun onItemClick(position: Int, type: Int) {
                    rvSelectBirthMonth.visibility = View.INVISIBLE
                    rvSelectBirthDay.visibility = View.INVISIBLE
                    rvSelectBirthYear.visibility = View.INVISIBLE
                    when (type) {
                        0 -> {
                            //月
                            imgSelectBirthMonthExpand.setImageResource(com.lib.module_res.R.mipmap.img_register_down)
                            txtSelectBirthMonth.text =
                                p.context.resources.getStringArray(R.array.month_array)
                                    .toMutableList()[position]
                        }

                        1 -> {
                            //日
                            imgSelectBirthDayExpand.setImageResource(com.lib.module_res.R.mipmap.img_register_down)
                            txtSelectBirthDay.text =
                                p.context.resources.getStringArray(R.array.day_array)
                                    .toMutableList()[position]
                        }

                        2 -> {
                            //年
                            imgSelectBirthYearExpand.setImageResource(com.lib.module_res.R.mipmap.img_register_down)
                            txtSelectBirthYear.text = p.yearList[position]
                        }
                    }

                }
            })
        }
    }

}